#ifndef __CHADEMOLOGICPROCESS_H__
#define __CHADEMOLOGICPROCESS_H__

#include "stm32f7xx_hal.h"


#define CHADEMOMSG_COUNTDOWN		400		//超时倒计时10s = 25ms * 400

typedef enum 
{
	CHADEMO_RPTINIT = 0,
	CHADEMO_START,
	CHADEMO_SSETUP,
	CHADEMO_CHRGPARADISC,
	CHADEMO_CABLECHK,
	CHADEMO_POWERDELIV,
	CHADEMO_CURRDMD,
	CHADEMO_WELDDETECT,
	CHADEMO_SSTOP,
	CHADEMO_ASTOP,
	CHADEMO_MSGNUM,
}RNL_ChademoSeccMsg;

#pragma pack(1)

//EVSE Status
typedef struct
{
	uint8_t StatusCode;		//@ref enum EVSE_STATUSCODE_TYPE
}evseStatusDef;
typedef struct
{
	uint8_t ReadyFlag; 
	uint8_t ErrorCode;
	uint8_t RESSSOC;
}evStatusDef;

typedef struct
{
	//ReportStateReq
	uint8_t reportStateType;//@ref enum ReportStateType
	
	//StartReq
	uint8_t chargeType; 	//@ref enum StartChargeType
	//SessionSetup
	uint8_t responseCode;	//@ref enum ResponseCodeType
	
	//ChargeParameterDiscoveryRes
	evseStatusDef evseStatus;
	uint32_t evseMaxCur;	 //0.1A
	uint32_t evseMaxVol;	 //0.1V
	
	//PowerDeliveryRes
	uint32_t evsePresentCur; //0.1A
	uint32_t evsePresentVol; //0.1V
	
	//CableCheckRes
	uint8_t evseProcess;	 //@ref enum EVSE_ProcessingType
	
	//CurrentDemandRes
	uint8_t currentLimitAchieved;
	uint8_t voltageLimitAchieved;
	
	//WeldingDetectionRes
	//SessionStopRes
	
}RNL_CHAdeMOEvsePara;

typedef struct
{
	//ReportInitRes
	uint8_t errorCode;	//@ref enum DC_EVErrorCodeType
	uint8_t majorVersion;
	uint8_t minorVersion;
	uint8_t releaseVersion;
	uint8_t infoLen;
	uint8_t rptInitInfo[24];

    //ReportSLAC
	uint8_t aveAtten;	//AverageAttenuation
	uint8_t pevMac[6];
	//ReportSDP
	uint16_t tcpPort;
	uint8_t secData;
	uint8_t tcpData;	
	
	//ReportStateRes
	uint8_t rptStateType;
	uint8_t rptStateInfo[3];
	
//	uint8_t chademoLock;
	uint8_t chademoPlugin;
	
	//AllStopRes
	uint8_t astInfo[4];
	
	//SessionSetup
	evStatusDef evStatus;
	
	//ChargeParameterDiscoveryReq
	uint32_t evMaxVol;

	//CableCheckReq
	
	//PowerDeliveryReq
	uint8_t rdyChgState;	//1:start	0:stop
	
	//CurrentDemandReq
	uint32_t evTargetCur;   // 需求电流
	uint32_t remainTimeFullSOC;
	uint32_t evTargetVol;   // 需求电压
	
	//WeldingDetectionReq
	
	//SessionStopReq

}RNL_CHAdeMOEvccPara;

//ccu——>plc(CHAdeMO)数据包发送标志
typedef union
{
    uint16_t    msgFlags;
    struct
    {
        uint16_t    sSetup		:1;	// 0 SessionSetupRequest
        uint16_t    chrgParaDisc:1;	// 1 ChargeParameterDiscoveryReq
		uint16_t    cableChk	:1;	// 2 CableCheckReq
        uint16_t    powerdeliv	:1;	// 3 PowerDeliveryReq
        uint16_t    curDemd		:1;	// 4 CurrentDemandReq
        uint16_t    weldDet		:1;	// 5 WeldingDetectionReq
        uint16_t    sStop		:1;	// 6 SessionStopReq
        uint16_t    aStop		:1;	// 7 AllStopRes
        uint16_t    rptInit		:1;	// 8 ReportInit
        uint16_t    start		:1;	// 9 Start
        uint16_t    rptState	:1;	// 10 ReportState
        uint16_t    reserve		:5; // 保留
    }flagBit;
}CHAdeMOTxFlag;

//plc——ccu>转换板(CHAdeMO)数据包接收标志
typedef union
{
    uint16_t    msgFlags;
    struct
    {
        uint16_t    sSetup		:1;	// 0 SessionSetupRequest
        uint16_t    chrgParaDisc:1;	// 1 ChargeParameterDiscoveryReq
		uint16_t    cableChk	:1;	// 2 CableCheckReq
        uint16_t    powerdeliv	:1;	// 3 PowerDeliveryReq
        uint16_t    curDemd		:1;	// 4 CurrentDemandReq
        uint16_t    weldDet		:1;	// 5 WeldingDetectionReq
        uint16_t    sStop		:1;	// 6 SessionStopReq
        uint16_t    aStop		:1;	// 7 AllStopRes
        uint16_t    rptInit		:1;	// 8 ReportInit
        uint16_t    rptState	:1;	// 9 ReportState
        uint16_t    rptV2g		:1;	// 10 ReportV2G
        uint16_t    rptSlac		:1;	// 11 ReportSLAC
        uint16_t    rptSdp		:1;	// 12 ReportSDP
        uint16_t    reserve		:3; // 保留
    }flagBit;
}CHAdeMORxFlag;

//瑞凯诺日标通讯控制结构体
typedef struct
{
	uint8_t chargingStep;	// 充电阶段标志
	CHAdeMOTxFlag sendFlag;	// 接收标志
	CHAdeMORxFlag rcvFlag;	// 发送标志
	CHAdeMORxFlag overTimeFlag;// 超时标志
	uint16_t overTimeCtrl[CHADEMO_MSGNUM];
	
}RNL_CHAdeMOCommunicationCtrlS;

#pragma pack()

extern RNL_CHAdeMOCommunicationCtrlS rnlChademoCommCtrl[];

// 瑞凯诺plc(CHAdeMO)充电阶段状态迁移函数
void RNL_SetChademoChargingStep(uint8_t gunId, uint8_t step);
uint8_t RNL_GetChademoChargingStep(uint8_t gunId);
// 瑞凯诺 plc(CHAdeMO)通讯初始化函数
void RNL_CHAdeMOCommunicationInit(uint8_t gunId);
// 瑞凯诺  plc(CHAdeMO)充电控制函数
void RNL_CHAdeMOChargingCtrlEntry(uint8_t gunId);
#endif //__CHADEMOLOGICPROCESS_H__
